package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.Address;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.AddressEditActivity;
import com.shaoshaohuo.app.ui.AddressManagerActivity;
import com.shaoshaohuo.app.utils.AreaUtil;

import java.util.List;

/**
 * 常用收获地址 页面 列表
 * 
 * @author cao
 *
 */
public class AddressAdapter extends BaseAdapter {

	private List<Address> list;
	private AddressManagerActivity activity;
	public Address defaultAddress;

	public AddressAdapter(Context context, List<Address> list) {
		this.list = list;
		this.activity = (AddressManagerActivity) context;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = View.inflate(activity, R.layout.item_address_manager, null);
		}
		ViewHolder holder = ViewHolder.getHolder(convertView);
		final Address address = list.get(position);
		holder.tv_name.setText(address.getReceiver_name());
		holder.tv_phone.setText(address.getReceiver_phone());
		if ("1".equals(address.getIs_default())) {
			defaultAddress = address;
			holder.tv_default_address.setVisibility(View.VISIBLE);
			holder.cb_default_address.setSelected(true);
		} else if ("0".equals(address.getIs_default())) {
			holder.tv_default_address.setVisibility(View.INVISIBLE);
			holder.cb_default_address.setSelected(false);
		}
		holder.tv_address.setText(AreaUtil.getAreaFullname(address.getCityid()));
		holder.tv_detail_address.setText(address.getAddress());
		// 删除地址
		holder.tv_delete_address.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				deleteAddress(address);
			}
		});
		// 编辑 地址 先删除
		holder.tv_edit_address.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				deleteAddress(address);
				Intent intent = new Intent(activity, AddressEditActivity.class);
				intent.putExtra(ExtraName.address, address);
				activity.startActivityForResult(intent, activity.FLAG_EDIT_ADDRESS);
			}
		});
		//设置默认收货地址
		holder.cb_default_address.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				activity.startLoadingDialog();
				RequestService.getInstance().setDefaultAddress(activity, address.getId(), BaseEntity.class, new RequestListener() {
					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						activity.dismissLoadingDialog();
						if(resultData.isOk()){
							if(defaultAddress!=null){
								defaultAddress.setIs_default("0");
							}
							defaultAddress = address;
							address.setIs_default("1");
							notifyDataSetInvalidated();
						}
					}
					@Override
					public void onFailed(int requestCode, Exception error, String msg) {
						activity.dismissLoadingDialog();
						activity.showToast(msg);
					}
				});
			}
		});
		return convertView;
	}

	private void deleteAddress(final Address address) {
		RequestService.getInstance().deleteAddress(activity, address.getId(), BaseEntity.class, new RequestListener() {
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				if (resultData.isOk()) {
					list.remove(address);
					if(address.equals(defaultAddress))
						defaultAddress=null;
					notifyDataSetChanged();
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {

			}
		});
	}

	static class ViewHolder {
		public TextView tv_name;
		public TextView tv_phone;
		public TextView tv_default_address;
		public TextView tv_address;
		public TextView tv_detail_address;
		public ImageView cb_default_address;
		public TextView tv_delete_address;
		public TextView tv_edit_address;

		public ViewHolder(View convertView) {
			tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
			tv_default_address = (TextView) convertView.findViewById(R.id.tv_default_address);
			tv_address = (TextView) convertView.findViewById(R.id.tv_address);
			tv_detail_address = (TextView) convertView.findViewById(R.id.tv_detail_address);
			cb_default_address = (ImageView) convertView.findViewById(R.id.cb_default_address);
			tv_delete_address = (TextView) convertView.findViewById(R.id.tv_delete_address);
			tv_edit_address = (TextView) convertView.findViewById(R.id.tv_edit_address);
			convertView.setTag(this);
		}

		public static ViewHolder getHolder(View convertView) {
			return (ViewHolder) (convertView.getTag() != null ? convertView.getTag() : new ViewHolder(convertView));
		}
	}

}
